<?php
namespace app\admin\controller;
use app\common\model;
use think\Exception;

class Sign extends Base
{
    public $signModel;
    public $meetingModel;
    public function __construct() {
        parent::__construct();
        $this->signModel = new model\Sign();
        $this->meetingModel = new model\Meeting;
    }

    public function index () {
        $where = [];
        $input = input();
        if($input['meeting_id']) {
            $where['meeting_id'] = $input['meeting_id'];
        }
        if($input['name']) {
            $where['name'] = $input['name'];
        }
        if($input['phone']) {
            $where['phone'] = $input['phone'];
        }
        if($input['idcard']) {
            $where['idcard'] = $input['idcard'];
        }
        $page = max($_GET['page'],1);
        $size = config('page_size');
        $datalist = $this->signModel->getPage($where,$page,$size,'create_time desc');
        foreach($datalist as $k=>$v) {
            $datalist[$k]['meeting'] = $this->meetingModel->getOne(['id'=>$v['meeting_id']]);
        }
        $pager = $this->signModel->where($where)->paginate($size)->render();
        $this->assign('pager', $pager);
        $this->assign('datalist',$datalist);

        $meetinglist = $this->meetingModel->getList();
        $this->assign('meetinglist',$meetinglist);

        return view();
    }

    public function add () {
        $input = input();
        if($input['id']) {
            $res = $this->signModel->getOne(['id'=>$input['id']]);
            $where = ['id'=>$input['id']];
        }
        if(IS_AJAX) {
            $data = $input;
            if($res) {
                $this->signModel->where($where)->update($data);
            }else{
                $insert = $data;
                $insert['create_time'] = getDateTime();
                $this->signModel->insert($insert);
            }
            return ajaxReturn(0,'保存成功');
        }
        $this->assign('data',$res);
        $this->view->engine->layout('common/login-layout'); 
        return view();
    }

    public function del () {
        $input = input();
        $res = $this->signModel->getOne(['id'=>$input['id']]);
        if(!$res) {
            return ajaxReturn(1,'数据不存在','');
        }
        $this->signModel->where(['id'=>$input['id']])->delete();
        return ajaxReturn(0,'已删除','');
    }


}
